﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using NHibernate;

namespace Apollo
{
    /// <summary>
    /// Summary description for BulkImportService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class BulkImportService : System.Web.Services.WebService
    {

        public BulkImportService()
        {

        }


        [WebMethod]
        public List<BulkImport> GetPendingImports() {
            Apollo.Application.Current.Initialize();

            List<BulkImport> imports = new List<BulkImport>(
                Apollo.Application.CurrentSession.QueryOver<BulkImport>()
                .Where(bi => bi.State == BulkImportState.Pending)
                .List());

            return imports;
        }

        [WebMethod]
        public BulkImportState BeginImport(BulkImport import) {
            BulkImport dbImport = Apollo.Application.CurrentSession.QueryOver<BulkImport>()
                .Where(bi => bi.Id == import.Id)
                .SingleOrDefault();

            if (dbImport == null)
                return BulkImportState.Failed;

            return dbImport.State;
        }

        [WebMethod]
        public void RollbackImport(BulkImport import)
        {
            BulkImport dbImport = Apollo.Application.CurrentSession.QueryOver<BulkImport>()
                .Where(bi => bi.Id == import.Id)
                .SingleOrDefault();

            if (dbImport == null) return;

            dbImport.State = import.State;
            dbImport.FailureReason = import.FailureReason;

            ITransaction tran = Apollo.Application.CurrentSession.BeginTransaction();

            try
            {
                Apollo.Application.CurrentSession.Save(dbImport);

                tran.Commit();
            }
            catch (Exception ex) {
                Apollo.Application.LogError(ex);

                tran.Rollback();
            }
        }

        [WebMethod]
        public void CommitImport(BulkImport import) {
            BulkImport dbImport = Apollo.Application.CurrentSession.QueryOver<BulkImport>()
                .Where(bi => bi.Id == import.Id)
                .SingleOrDefault();

            if (dbImport == null) return;

            dbImport.State = import.State;
            dbImport.FailureReason = import.FailureReason;

            ITransaction tran = Apollo.Application.CurrentSession.BeginTransaction();

            try
            {
                Apollo.Application.CurrentSession.Save(dbImport);

                tran.Commit();
            }
            catch (Exception ex)
            {
                Apollo.Application.LogError(ex);

                tran.Rollback();
            }
        }

        [WebMethod]
        public BulkImportRow ImportRow(BulkImport import, List<string> rowString) {
            BulkImport dbImport = Apollo.Application.CurrentSession.QueryOver<BulkImport>()
                .Where(bi => bi.Id == import.Id)
                .SingleOrDefault();

            if (dbImport == null)
                return null;


            return null;
        }
    }
}
